package org.hgvc.webapp.ets.jsf.beans.admin;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.faces.model.SelectItem;

import org.apache.commons.lang.ArrayUtils;
import org.hgvc.webapp.ets.bo.Agent;
import org.hgvc.webapp.ets.bo.Agreement;
import org.hgvc.webapp.ets.bo.Member;
import org.hgvc.webapp.ets.constant.BookingConstant;
import org.hgvc.webapp.ets.spring.service.MemberService;
import org.hgvc.webapp.ets.spring.service.SystemService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.SpringUtils;

public class AdminAgreementTerminateBean extends AbstractBean {
	public static final String NAV_LIST = "adminAgreementTerminate";
	public static final String BEAN_NAME = "adminAgreementTerminateBean";
	public static final String NAV_ACTION_BULETIN = "actionBuletinNavApproveTermination";
	
	
	private Member member;
	private List<Agreement> agreements;
	private MemberService memberService;
	private String affectQuota;
	private String affectCommission;
	private double refundToMember;
	private String actionNo;
	
	public String getActionNo() {
		return actionNo;
	}


	public void setActionNo(String actionNo) {
		this.actionNo = actionNo;
	}


	public List<SelectItem> getAffect() {
		List<SelectItem> selectList = new ArrayList<SelectItem>();
		
		SelectItem item = new SelectItem("Y","Yes");
		SelectItem item2 = new SelectItem("N","No"); 
		selectList.add(item); 
		selectList.add(item2);

		
		return selectList;
	}

	
	public String getAffectQuota() {
		return affectQuota;
	}

	public void setAffectQuota(String affectQuota) {
		this.affectQuota = affectQuota;
	}

	public String getAffectCommission() {
		return affectCommission;
	}

	public void setAffectCommission(String affectCommission) {
		this.affectCommission = affectCommission;
	}

	public double getRefundToMember() {
		return refundToMember;
	}

	public void setRefundToMember(double refundToMember) {
		this.refundToMember = refundToMember;
	}

	public List<Agreement> getAgreements() {
		return agreements;
	}
	
	public String actionBuletinNavApproveTermination(String actionNo, String... params) throws Exception{
		affectCommission = "";
		affectQuota = "";
		refundToMember = 0.00;
		String[] agreementNo = params[1].split(",");
		List<String> agreementNos = Arrays.asList(agreementNo);
		member = null;
		agreements = new ArrayList<Agreement>();
		List<Agreement>agreementsAll = memberService.getActiveAgreementsByMember(params[2]);
		member = (Member)commonDao.getObject(Member.class, params[2]);
		 
		for(Agreement agreement	: agreementsAll){
			if (agreementNos.contains(agreement.getAgreetmentNo())){
				agreements.add(agreement);
			}
		} 
		this.actionNo = actionNo;
		 
		return "agreement_termination".concat("_"+getRoleName());
	}
	
	public String actionBuletinApproveTermination() throws Exception{
//		try{
			memberService.saveTerminateAgreement(agreements,affectQuota, affectCommission, refundToMember, super.getThisUser());
			SystemService systemService = (SystemService) SpringUtils.lookupBean("systemService");
			systemService.deleteActionBulletinByActionNo(actionNo);
//		}catch(BaseException be){
//			initErrorBean(be);
//		}
		return "financePortal";
	}
	
	public String cancelTermination() throws BaseException{
		SystemService systemService = (SystemService) SpringUtils.lookupBean("systemService");
		systemService.deleteActionBulletinByActionNo(actionNo);
		return "financePortal";
	}
	
	public void setAgreements(List<Agreement> agreements) {
		this.agreements = agreements;
	}

	public AdminAgreementTerminateBean() {
	}
	
	public void iniBeanForPreTermination(Member member) throws BaseException{
		memberService = (MemberService)SpringUtils.lookupBean("memberService");
		this.member = member;
		agreements = memberService.getActiveAgreementsByMember(member.getMemberId());
	}
	 
	public String cancelAction(){
		return AdminMemberMgmtBean.NAV_LIST.concat("_"+getRoleName());
	}
	
	public String terminateAction() throws Exception{
		try{
			memberService.savePreTerminateAgreement(agreements);
		}catch(BaseException be){
			initErrorBean(be);
			return NAV_LIST.concat("_"+getRoleName());
		}
		return cancelAction();
	}
	
	public Member getMember() {
		return member;
	}
	public void setMember(Member member) {
		this.member = member;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}
	
	
}
